

function factory (UserModel, ArticleModel) {
  return function (req, res) {
    var page1 = [], betters = [], hots = [], articlesNumber = 0, newUsers = [], greatUsers =[]
    ArticleModel.find({}, function (err, arts) {
      articlesNumber = arts.length
      page1 = arts.sort(function (o1, o2) {
        return o2.time - o1.time
      }).slice(0, 10)
      hots = arts.sort(function (o1, o2) {
        return (o2.good + o2.collected - o2.bad) - (o1.good + o1.collected - o1.bad)
      }).slice(0, 5)
      betters = arts.filter(function (item) {
        return +item.better
      })
      UserModel.find({}, function (err, users) {

        newUsers = users.sort(function (o1, o2) {
          return o2.time - o1.time
        }).slice(0, 8)
        greatUsers = users.sort(function (o1, o2) {
          return o2.integral - o1.integral
        }).slice(0, 5)

        res.send({articlesNumber: articlesNumber,page1: page1, betters: betters, hots: hots,newUsers: newUsers,greatUsers: greatUsers})
      })
      

    })
  }
}

module.exports = factory